﻿package mortal.game.view.common.guide.stepOperater
{
    import com.gengine.debug.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.guide.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AutoGuideWindowSteper extends AutoGuideStepBase implements IAutoGuideSteper
    {
        private var _guideWin:Window;
        private var _guideChild:DisplayObject;

        public function AutoGuideWindowSteper()
        {
            return;
        }// end function

        public function guide(param1:AutoGuideData, param2:Window = null) : void
        {
            if (param1 == null)
            {
                return;
            }
            if (param1.guideType != AutoGuideType.Window || param1.guideTypeValue == null || param1.guideTypeValue == "")
            {
                return;
            }
            if (param2 == null)
            {
                param2 = DisplayUtil.getChildByPropety(LayerManager.windowLayer, "winTitleName", param1.guideTypeValue) as Window;
            }
            if (param2 == null)
            {
                param2 = DisplayUtil.getChildByPropety(LayerManager.windowLayer3D, "winTitleName", param1.guideTypeValue) as Window;
            }
            if (param2 == null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideDelDoingRecord, param1));
                return;
            }
            if (_working)
            {
                this.stop();
            }
            _data = param1;
            var _loc_3:* = DisplayUtil.getChildByPropety(param2, "name", param1.clickChildName as String);
            if (_loc_3 == null)
            {
                _loc_3 = DisplayUtil.getChildByPropety(param2.contentTopOf3DSprite, "name", param1.clickChildName as String);
            }
            if (_loc_3 == null)
            {
                _loc_3 = DisplayUtil.getChildByPropety(param2.contentContainer, "name", param1.clickChildName as String);
            }
            if (_loc_3 == null)
            {
                Log.debug("窗口自动指引，在窗口：" + param2["winTitleName"] + "中找不到name为：" + param1.clickChildName + " 的child， 指引中断");
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, param1));
                return;
            }
            this._guideChild = _loc_3;
            this._guideWin = param2;
            if (param1.needMask)
            {
                showMask(param2.highestContentContainer, _loc_3);
            }
            if (param1.needArrow)
            {
                showArrowByChild(param2.highestContentContainer, param1, _loc_3);
            }
            if (param1.needLightMask)
            {
                showLightMask(param2.highestContentContainer, _loc_3);
            }
            if (param1.clickType == AutoGuideClickType.Click)
            {
                this._guideChild.addEventListener(MouseEvent.CLICK, this.clickWinChildHandler, false, 0, true);
            }
            else
            {
                this._guideChild.addEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, false, 0, true);
            }
            this._guideWin.addEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            super.setData(param1, param2.highestContentContainer, this._guideChild);
            return;
        }// end function

        override protected function guideTimerOut() : void
        {
            if (this._guideWin != null)
            {
                this._guideWin.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
                if (this._guideWin is IView)
                {
                    (this._guideWin as IView).hide();
                }
            }
            if (this._guideChild != null)
            {
                this._guideChild.removeEventListener(MouseEvent.CLICK, this.clickWinChildHandler, false);
                this._guideChild.removeEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, false);
            }
            super.guideTimerOut();
            this._guideWin = null;
            return;
        }// end function

        private function winCloseHandler(event:WindowEvent) : void
        {
            this.stop();
            if (this._guideChild.name != "Window_Btn_Close" && GuideController.shouldResumeToTaskAfterGuide() && _data.filterName != "NoMainEndType")
            {
                GameController.guide.resumeToTask();
            }
            return;
        }// end function

        private function clickWinChildHandler(event:MouseEvent) : void
        {
            this.stop(_data);
            return;
        }// end function

        override public function stop(param1:AutoGuideData = null) : void
        {
            super.stop(param1);
            if (this._guideWin != null)
            {
                this._guideWin.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
                this._guideWin = null;
            }
            if (this._guideChild != null)
            {
                this._guideChild.removeEventListener(MouseEvent.CLICK, this.clickWinChildHandler, false);
                this._guideChild.removeEventListener(MouseEvent.DOUBLE_CLICK, this.clickWinChildHandler, false);
            }
            if (param1 != null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, param1));
            }
            return;
        }// end function

    }
}
